package com.masterclient.app.businessmodule.homemodule.presenter.timetask.search;
import android.os.Handler;
import android.os.Message;

import com.eoner.baselib.utils.notify.RxBus;
import com.eoner.common.config.Config;
import com.masterclient.app.businessmodule.homemodule.presenter.timetask.base.HomeBaseTimerTask;

import java.util.List;

public class HomeSearchWordTimerTask extends HomeBaseTimerTask<HomeSearchWordTimerTask> {

    //搜索词
    private Handler mHandler;
    //数组
    private List<String> searchTermKeyword;
    //默认执行的秒数
    private int searchKeywordCycleTime = 2; //秒
    //数据索引
    private int keyword_position = 0;
    private RxBus rxBus;

    public HomeSearchWordTimerTask() {
        rxBus =  RxBus.$();
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 1:
                        if (searchTermKeyword != null) {
                            rxBus.post(Config.SEARCH_HINT, searchTermKeyword.get(keyword_position));

                            if (getHomeMainView() != null) {
                                if (searchTermKeyword.size() > 1) {
                                    getHomeMainView().refreshHomeDataToTopSearchView(searchTermKeyword.get(keyword_position),true);
                                } else {
                                    getHomeMainView().refreshHomeDataToTopSearchView(searchTermKeyword.get(keyword_position),false);
                                }
                            }
                            if (keyword_position >= searchTermKeyword.size() - 1) {
                                keyword_position = 0;
                            } else {
                                keyword_position++;
                            }
                        }
                        mHandler.sendEmptyMessageDelayed(1, searchKeywordCycleTime * 1000);
                        break;
                }
            }
        };
    }

    public void executeHomeSearchTimerTask(List<String> searchTermKeyword,int cycleTime) {
        this.keyword_position = 0;
        this.searchTermKeyword = searchTermKeyword;
        this.searchKeywordCycleTime = cycleTime;
        if (mHandler != null) {
            mHandler.sendEmptyMessage(1);
        }
    }

    @Override
    public void stopTimerTask() {
        searchTermKeyword = null;
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
    }
}
